package com.csproj.basic.repository.impl;

import com.csproj.basic.domain.Site;
import com.csproj.basic.repository.custom.SiteRepositoryCustom;
import com.csproj.example.domain.Organization;
import com.csproj.example.repository.custom.OrganizationRepositoryCustom;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import java.util.HashMap;
import java.util.Map;

/**
 * Spring Data JPA repository for the ClassInfo entity.
 */
@SuppressWarnings("unused")
public class SiteRepositoryImpl implements SiteRepositoryCustom {
	@PersistenceContext
    private EntityManager em;

	public EntityManager getEm() {
		return em;
	}

	/**
	 * 站点分页查询方法
	 * @param map	参数
	 * @param pageable		分页对象
	 * @return
	 */
    public Page<Site> findPageList(Map<String, Object> map, Pageable pageable) {
        //拼hql
        String hql = " from Site s where s.deletedFlag = 0 ";
        Map<String,Object> queryParams = new HashMap<>();

        String siteName = (String) map.get("siteName");
        if (StringUtils.isNotBlank(siteName)) {
            hql += " and s.siteName like :siteName";
            queryParams.put("siteName","%"+siteName+"%");
        }

        String siteType = (String) map.get("siteType");
        if (StringUtils.isNotBlank(siteType)) {
            hql += " and s.siteType = :siteType";
            queryParams.put("siteType", siteType);
        }

        //查询
        Page<Site> fieldList = query(hql, pageable, queryParams);
        //返回
        return fieldList;
    }

}
